/**
 * 消息列表
 *
 */
define(['app', 'comm/pager/PagerControl', 'js/services/MessageBox', 'js/services/MuseService',
    'js/directives/DatetimePicker', 'js/controllers/common/modal/messagelog/MessageLogModalController',
    'js/filters/StrLimitFilter'], function (app) {

    var deps = ['$scope', '$state', '$modal', 'MessageBox', 'MuseService'];

    function controller ($scope, $state, $modal, MessageBox, MuseService) {
        //初始化查询对象 和 分页对象
        $scope.queryObj = {
            'sort': 'desc',
            'isHasRobot': false//是否包含机器人
        };
        $scope.pagerObj = {
            //当前页号
            currentPage: 1,
            // 每页显示条数
            pageSize: 15,
            //总记录数
            totalCount: -1
        };

        //获取列表
        $scope.getObjList = function () {
            //封装查询条件
            $scope.pagerObj.searchMap = $scope.queryObj;
            if ($scope.queryObj.createTimeStart > $scope.queryObj.createTimeEnd) {
                MessageBox.notify("结束时间不能小于开始时间");
                return;
            }

            MuseService.post('admin/messagelog/list.json', $scope.pagerObj).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.logList = data.data.itemList;
                        getDic();
                        //分页器
                        $scope.pagerObj = PageCtrl().page($scope.pagerObj, data.totalPage, $scope.pagerObj.currentPage);
                    } else {
                        MessageBox.notify(data.message);
                    }
                });
        };

        $scope.getObjList();

        //重置查询表单
        $scope.resetParam = function () {
            $scope.queryObj = {
                'sort': 'desc',
                'isHasRobot': false//是否包含机器人
            };
            $scope.pagerObj = {
                pageSize: 15,
                currentPage: 1
            };
        };

        //查看上下文
        $scope.view = function (obj) {
            obj.userName = obj.question == '' ? obj.userName : ((obj.uname == undefined || obj.uname == '') ? obj.cellphone : obj.uname);
            var modalInstance = $modal.open({
                templateUrl: 'views/common/modal/messagelog/MessageLogModal.html',
                controller: 'MessageLogModalController',
                backdrop: 'static',
                keyboard: false,
                //size:'lg',
                resolve: {
                    selObj: function () { //传递参数给MultiChargeController
                        return obj;
                    }
                },
                windowClass: 'app-modal-window'
            });
        };

        $scope.pageChanged = function () {
            PageCtrl().check($scope.pagerObj);
            $scope.getObjList();
        };

        function getDic () {
            for (var i = 0; i < $scope.logList.length; i++) {
                for (var j = 0; j < $scope.dic.length; j++) {
                    if ($scope.logList[i].matchingDegree == $scope.dic[j].name) {
                        $scope.logList[i].matchingDegreeName = $scope.dic[j].value;
                    }
                }

            }
        }

        $scope.dic = [
            {
                'name': '0',
                'value': '聊天库库匹配'
            },
            {
                'name': '1',
                'value': '无匹配结果'
            },
            {
                'name': '2',
                'value': '中低度匹配'
            },
            {
                'name': '3',
                'value': '高度匹配'
            },
            {
                'name': '4',
                'value': '完全匹配'
            }
        ];
    }

    controller.$inject = deps;
    app.lazy.controller('MessageLogListController', controller);
});
